package examples.mail;

import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

public class HttpMailCheck {

	/**
	 * 163.com 126.com yeah.com
	 */
	public static String check163(String add) throws Exception {
		String[] tmp = add.split("@");
		String name = tmp[0];
		String att = tmp[1];
		Connection conn = Jsoup
				.connect("http://reg.email.163.com/unireg/call.do?cmd=urs.checkName");
		// conn.header("Connection", "close");

		conn.data("name", name);
		Response resp = conn.execute();
		String ret = resp.body();
		System.out.println(ret);

		if (ret.indexOf("\"desc\":\"OK\"") > 0) {
			if (ret.contains(att)) {
				return "not found";
			} else {
				return "1";
			}
		} else {
			throw new Exception("ret error");
		}
	}

	public static String checkSina(String add) throws Exception {
		String[] tmp = add.split("@");
		String name = tmp[0];
		String att = tmp[1];
		String[] tmp1 = att.split("\\.");
		String cncom = tmp1[1];

		Connection conn = Jsoup
				.connect("http://login.sina.com.cn/signup/check_user.php");

		conn.header("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		conn.header("Accept-Encoding", "gzip, deflate");
		conn.header("Accept-Language", "en-us,en;q=0.5");
		conn.header("Cache-Control", "no-cache");
		conn.header("Connection", "keep-alive");
		conn.header("Content-Type",
				"application/x-www-form-urlencoded; charset=UTF-8");
		conn.header("Host", "login.sina.com.cn");
		conn.header("Pragma", "no-cache");
		conn.header("Referer",
				"https://login.sina.com.cn/signup/signup.php?entry=freemail");
		conn.header("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
		conn.header("X-Requested-With", "XMLHttpRequest");
		conn.data("from", "regmail");
		conn.data("name", name);
		conn.request().method(Method.POST);
		Response resp = conn.execute();
		Map<String, String> m = resp.headers();
		String ret = resp.body();
		System.out.println(ret);
		if (ret.contains("cn") && ret.contains("com")) {
			if (ret.contains(cncom + " yes")) {
				return "1";
			} else {
				return "not found";
			}
		} else {

			throw new Exception("ret error");
		}
	}

	// http://fmail.21cn.com/freeinterface/jsp/reg/checkUserExist.jsp?clientid=userName&userName=sdfsdfd&_=1351211766062
	public static String check21cn(String add) throws Exception {
		String[] tmp = add.split("@");
		String name = tmp[0];
		String att = tmp[1];
		Connection conn = Jsoup
				.connect("http://fmail.21cn.com/freeinterface/jsp/reg/checkUserExist.jsp?clientid=userName&userName="
						+ name + "");
		Response resp = conn.execute();
		String ret = resp.body().trim();
		System.out.println(ret);

		if (ret.contains("0")) {
			return "not found";
		} else if (ret.contains("-2")) {
			return "1";
		} else {
			throw new Exception("ret error");
		}

	}

	public static void main(String[] args) throws Exception {
		java.security.Security.setProperty("networkaddress.cache.ttl", "0");

		System.setProperty("networkaddress.cache.ttl", "0");

		System.setProperty("sun.net.spi.nameservice.provider.1",
				"dns,MyNameService");
		// boolean b = HttpMailCheck.check163("qqjava@163.com");
		QueryRunner qr = Db.getQueryRunner();
		// att='163.com' or att='126.com' or att='yeah.net' or
		String sql = "  select too from acc_csdn where ( att='21cn.com' or att='sina.com1'  or att='163.com' or att='126.com' or  att='yeah.net' )and stat='0' "
				+ "  limit 10000000   ";

		List<Object> tos = qr.query(sql, new ColumnListHandler("too"));

		for (Object o : tos) {
			//Thread.sleep(800);
			String to = (String) o;
			String stat;
			try {

				if (to.indexOf("sina.com") > -1) {
					stat = HttpMailCheck.checkSina(to);
				} else if(to.indexOf("163.com") > -1 || to.indexOf("126.com") > -1 || to.indexOf("yeah.net") > -1) {
					stat = HttpMailCheck.check163(to);
				}else if (to.indexOf("21cn.com") > -1){
					stat=HttpMailCheck.check21cn(to);
				}else{
					stat="----";
				}

				qr.update(" update acc_csdn set stat = ? where too = ? ", stat,
						to);

			} catch (SocketTimeoutException ste) {
				ste.printStackTrace();
				continue;
			} catch (Exception e) {
				e.printStackTrace();
				stat = e.getMessage();
				qr.update(" update acc_csdn set stat = ? where too = ? ", stat,
						to);
				System.out.println(System.currentTimeMillis());
				System.exit(0);
				//Util.changeip();
			}

			System.out.println(to + "--->" + stat);
		}

	}
}